package com.xinyue.network.hamdlermapping;

import com.xinyue.network.codec.GameMessageContext;
import com.xinyue.network.codec.GameMessageHeader;
import com.xinyue.network.codec.IGameMessage;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author 王广帅
 * @date 2021年01月26日 4:26 下午
 */
public class GameChannelContext {
    private ChannelHandlerContext ctx;
    private GameMessageContext gameMessageContext;

    public GameChannelContext(ChannelHandlerContext ctx, GameMessageContext gameChannelContext) {
        this.ctx = ctx;
        this.gameMessageContext = gameChannelContext;
    }

    public void sendGameMessage(IGameMessage gameMessage) {
        GameMessageHeader header = gameMessageContext.getHeader();
        GameMessageContext newContext = new GameMessageContext(header);
        newContext.setGameMessage(gameMessage);
        header.setSendTime(System.currentTimeMillis());
        ctx.writeAndFlush(newContext);
    }

    public void sendError(IGameError error) {
        GameMessageHeader header = gameMessageContext.getHeader();
        header.setErrorCode(error.getErrorCode());
        GameMessageContext newContext = new GameMessageContext(header);
        ctx.writeAndFlush(newContext);
    }
}
